package com.example.a_app;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private ImageView imageView;
    private EditText et_name;
    private RadioGroup radio_group;
    private EditText et_minzu;
    private EditText et_id;
    private EditText et_add;
    private Button button_submit;
    private RadioButton radio_man;
    private RadioButton radio_wuman;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        imageView = (ImageView) findViewById(R.id.imageView);
        et_name = (EditText) findViewById(R.id.et_name);
        radio_group = (RadioGroup) findViewById(R.id.radio_group);
        et_minzu = (EditText) findViewById(R.id.et_minzu);
        et_id = (EditText) findViewById(R.id.et_id);
        et_add = (EditText) findViewById(R.id.et_add);
        button_submit = (Button) findViewById(R.id.button_submit);

        button_submit.setOnClickListener(this);
        radio_man = (RadioButton) findViewById(R.id.radio_man);
        radio_man.setOnClickListener(this);
        radio_wuman = (RadioButton) findViewById(R.id.radio_wuman);
        radio_wuman.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_submit:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String name = et_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "name不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String sex="";
        if(radio_man.isChecked()){
            sex=radio_man.getText().toString().trim();
        }else{
            sex=radio_wuman.getText().toString().trim();
        }

        String minzu = et_minzu.getText().toString().trim();
        if (TextUtils.isEmpty(minzu)) {
            Toast.makeText(this, "minzu不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String id = et_id.getText().toString().trim();
        if (TextUtils.isEmpty(id)) {
            Toast.makeText(this, "id不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String add = et_add.getText().toString().trim();
        if (TextUtils.isEmpty(add)) {
            Toast.makeText(this, "add不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        Student student=new Student(name,sex,minzu,id,add);
        student.save(this);


    }
}
